package dice.sinanya.db.custom;

import dice.sinanya.db.tools.DbUtil;
import dice.sinanya.entity.EntityCustomSearch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nlpcn.commons.lang.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import static dice.sinanya.db.rules.Rule.makeArray;
import static dice.sinanya.tools.getNLP.getNlp;
import static dice.sinanya.tools.makedata.StringFormatById.stringFormatById;


/**
 * @author SitaNya
 * 日期: 2019-06-15
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明: 查询KP主群类，刷写到静态变量中，只在静态变量中找不到时才需要使用
 */
public class SelectCustomSearch {
    private static final Logger log = LogManager.getLogger(SelectCustomSearch.class.getName());


    public SelectCustomSearch() {
        //        初始化时无需逻辑
    }

    /**
     * 刷新kp主群设定到静态变量中，只有静态变量中找不到某人的kp主群记录时才会使用
     */
    public boolean checkCanInput(String key, String authorId) {
        boolean canInput = true;
        try (Connection conn = DbUtil.getConnection()) {
            String sql = "select authorId,`key` from coc.customSearch";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                try (ResultSet set = ps.executeQuery()) {
                    while (set.next()) {
                        if (key.equals(set.getString("key")) && !authorId.equals(set.getString("authorId"))) {
                            canInput = false;
                        }
                    }
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return canInput;
    }

    /**
     * 刷新kp主群设定到静态变量中，只有静态变量中找不到某人的kp主群记录时才会使用
     */
    public boolean checkOverWrite(String key, String authorId) {
        boolean overWrite = false;
        try (Connection conn = DbUtil.getConnection()) {
            String sql = "select authorId,`key` from coc.customSearch";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                try (ResultSet set = ps.executeQuery()) {
                    while (set.next()) {
                        if (key.equals(set.getString("key")) && !authorId.equals(set.getString("authorId"))) {
                            overWrite = true;
                        }
                    }
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return overWrite;
    }

    public String getCustomMessages(String key) {
        EntityCustomSearch entityCustomSearch = new EntityCustomSearch();
        try (Connection conn = DbUtil.getConnection()) {
            String sql = "select * from coc.customSearch where `key`=? and enable=1";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, key);
                try (ResultSet set = ps.executeQuery()) {
                    if (set.next()) {
                        entityCustomSearch.setInputDate(set.getTimestamp("inputDate"));
                        entityCustomSearch.setKey(set.getString("key"));
                        entityCustomSearch.setMessages(set.getString("messages"));
                        entityCustomSearch.setAuthorId(set.getString("authorId"));
                        entityCustomSearch.setAuthorNick(set.getString("authorNick"));
                        return entityCustomSearch.build();
                    }
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        ArrayList<String> keyList = makeArray(getNlp(key));
        ArrayList<String> result = new ArrayList<>();
        try (Connection conn = DbUtil.getConnection()) {
            for (String singleKey : keyList) {
                String sql = stringFormatById("select * from coc.customSearch where `key` like '%%%s%%' and enable=1", singleKey);
                try (PreparedStatement ps = conn.prepareStatement(sql)) {
                    try (ResultSet set = ps.executeQuery()) {
                        while (set.next()) {
                            String skill = set.getString("key");
                            if (!skill.contains("术") && !skill.contains("之")) {
                                result.add(set.getString("key"));
                            }
                        }
                    }
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return "不存在此条目:\t" + key + "\n猜测是以下条目:\n" + StringUtil.joiner(result, "\n");
    }
}
